package com.hbun.cxcy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hbun.cxcy.common.utils.PageUtils;
import com.hbun.cxcy.common.utils.Query;
import com.hbun.cxcy.entity.Activity;
import com.hbun.cxcy.mapper.ActivityMapper;
import com.hbun.cxcy.service.ActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbun.cxcy.utils.MarkdownUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mahaunyu
 * @since 2022-01-24
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<Activity> wrapper = new QueryWrapper<Activity>();
        wrapper.eq("status",1).orderByDesc("create_time");
        IPage<Activity> page = this.page(new Query<Activity>().getPage(params),
               wrapper);

        return new PageUtils(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveWithCustom(Activity activity) {

        activity.setCreateTime(new Date());
        activity.setUpdateTime(new Date());
        this.save(activity);
    }

    @Override
    public void updateByIdWithCustom(Activity activity) {
        activity.setUpdateTime(new Date());
        this.updateById(activity);
    }

    @Override
    public Activity getByIdWithCustom(Long activityId) {
        // 内容以markdown形式展示
        Activity activity = this.baseMapper.selectById(activityId);
        String markdownToHtml = MarkdownUtils.markdownToHtml(activity.getContent());
        activity.setContent(markdownToHtml);
        return activity;
    }
}
